﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections.Specialized;

/// <summary>
/// Summary description for PageBase
/// </summary>
public class PageBase : System.Web.UI.Page
{
    public PageBase()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    protected override void OnError(EventArgs e)
    {
        base.OnError(e);

        Exception appError = Server.GetLastError();
        StringBuilder builder = new StringBuilder();
        try
        {
            //添加错误
            builder.AppendFormat("{0} ========== 错误信息 ========== {0} ", Environment.NewLine);
            builder.AppendFormat(appError.Message);
            builder.AppendFormat("{0} ========== 堆栈信息 =========={0} ", Environment.NewLine);
            builder.AppendFormat(appError.StackTrace + Environment.NewLine);

            if (HttpContext.Current == null || Request == null)
            {
                builder.AppendFormat("{0} HttpContext.Current.Request对象为空 {0} ", Environment.NewLine);
            }
            else
            {
                //如果Request对象不为空，则从Request中获取服务器变量，查询变量，表单变量
                NameValueCollection serverVariables = null;
                if (Request.ServerVariables != null)
                {
                    serverVariables = Request.ServerVariables;
                }

                builder.AppendFormat("{0}{0} ========== 服务器端变量 =========={0} ", Environment.NewLine);
                if (serverVariables != null && serverVariables.Count > 0)
                {
                    for (int i = 0; i < serverVariables.Count; i++)
                    {
                        string tempValue = string.Empty;
                        foreach (string item in serverVariables.GetValues(i))
                        {
                            tempValue += string.Format("{0},", item);
                        }
                        builder.AppendFormat(@"{0} [{1}] [key:{2}]  [value:{3}] {0}", Environment.NewLine, i, serverVariables.GetKey(i), tempValue);
                    }
                }
                else
                {
                    builder.AppendFormat(@" 空 {0}", Environment.NewLine);
                }

                NameValueCollection queryStringVariables = null;
                if (Request.QueryString != null)
                {
                    queryStringVariables = Request.QueryString;
                }
                builder.AppendFormat("{0}{0} ========== 查询字符串变量 =========={0} ", Environment.NewLine);
                if (queryStringVariables != null && queryStringVariables.Count > 0)
                {
                    for (int i = 0; i < queryStringVariables.Count; i++)
                    {
                        string tempValue = string.Empty;
                        foreach (string item in queryStringVariables.GetValues(i))
                        {
                            tempValue += string.Format("{0},", item);
                        }
                        builder.AppendFormat(@"{0} [{1}] [key:{2}]  [value:{3}] {0}", Environment.NewLine, i, queryStringVariables.GetKey(i), tempValue);
                    }
                }
                else
                {
                    builder.AppendFormat(@" 空 {0}", Environment.NewLine);
                }

                NameValueCollection formgVariables = null;
                if (Request.QueryString != null)
                {
                    formgVariables = Request.QueryString;
                }
                builder.AppendFormat("{0} ========== 表单变量 ========== {0} ", Environment.NewLine);
                if (formgVariables != null && formgVariables.Count > 0)
                {
                    for (int i = 0; i < formgVariables.Count; i++)
                    {
                        string tempValue = string.Empty;
                        foreach (string item in formgVariables.GetValues(i))
                        {
                            tempValue += string.Format("{0}", item);
                        }
                        builder.AppendFormat(@"{0} [{1}] [key:{2}]  [value:{3}] {0}", Environment.NewLine, i, formgVariables.GetKey(i), tempValue);
                    }
                }
                else
                {
                    builder.AppendFormat(@" 空 {0}", Environment.NewLine);
                }
            }

            //EmailSender.Send("mynameisjiagang@163.com", "Reunion Error", builder.ToString());
        }
        catch (Exception ee)
        {
            Response.Write(builder.ToString());
        }
        // Clear the error from the server
        Server.ClearError();
    }
}